package com.org.testproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class MultiTouchTest extends Activity implements OnTouchListener{
	TextView textView;
	StringBuilder builder = new StringBuilder();
	
	float[] x = new float[10];
	float[] y = new float[10];
	boolean[] touched = new boolean[10];
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		textView = new TextView(this);
		textView.setText("Touch and drag (multiple fingers supported!");
		textView.setOnTouchListener(this);
		setContentView(textView);
	}

	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		// TODO Auto-generated method stub
		int action = arg1.getAction() & MotionEvent.ACTION_MASK;
		
		int pointerIndex = (arg1.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
		int pointerId = arg1.getPointerId(pointerIndex);
		
		switch(action)
		{
		case MotionEvent.ACTION_DOWN:
		case MotionEvent.ACTION_POINTER_DOWN:
			touched[pointerId] = true;
			x[pointerId] = (int)arg1.getX(pointerIndex);
			y[pointerId] = (int)arg1.getY(pointerIndex);
			break;
		
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_POINTER_UP:
		case MotionEvent.ACTION_CANCEL:
			touched[pointerId] = false;
			x[pointerId] = (int)arg1.getX(pointerIndex);
			y[pointerId] = (int)arg1.getY(pointerIndex);
			break;
			
		case MotionEvent.ACTION_MOVE:
			int pointerCount = arg1.getPointerCount();
			
			for(int i=0; i<pointerCount; i++)
			{
				pointerIndex = i;
				pointerId = arg1.getPointerId(pointerIndex);
				x[pointerId] = (int)arg1.getX(pointerIndex);
				y[pointerId] = (int)arg1.getY(pointerIndex);
			}
			break;			
		}
		updateTextView();
		return true;
	}
	
	private void updateTextView()
	{
		builder.setLength(0);
		
		for(int i=0; i<10; i++)
		{
			builder.append(touched[i]);
			builder.append(", ");
			builder.append(x[i]);
			builder.append(", ");
			builder.append(y[i]);
			builder.append("\n");
		}
		
		textView.setText(builder.toString());
	}

	
}
